import md5 from 'md5'


let context = null
const initContext = (vue) => {
	context = vue;
}

//GPS定位
const getGPS = async (address, token) => {
	let reqparam = {
		Address: address
	};
	let reqjsonStr = JSON.stringify(reqparam);
	let [error, result] = await context.commService.postEntityAsync("GetGPS", reqjsonStr, token);
	if (!error && result.statusCode == 200) {
		return result.data;
	}
	return null;
}

const geoMultipParamMapAsync = async (baseurl, path, queryparams) => {
	let qqSign = createMultipParamQQSign(`${path}?`, queryparams);
	let postdata = {
		key: context.qqMapKey,
		sig: qqSign
	};
	
	Object.keys(queryparams).forEach(function(key){
		 postdata[key] = queryparams[key];
	});

	let [error, result] = await uni.request({
		url: `${baseurl}${path}`,
		data: postdata
	});
	if (!error && result.statusCode == 200) {
		return result.data;
	}
	return null;
}

const createMultipParamQQSign = (url, queryparams) => {
	let params = {
		key: context.qqMapKey,
	};
	
	Object.keys(queryparams).forEach(function(key){
		 params[key] = queryparams[key];
	});

	let sortparams = sortObject(params);
	let paramStr = getQuerySortMd5(sortparams);

	paramStr = url + paramStr;
	console.log(paramStr);
	let md5Sign = md5(paramStr);
	console.log(md5Sign);
	return md5Sign;
}


//"https://apis.map.qq.com/ws/geocoder/v1",
const geoCommMapAsync = async (baseurl, path, queryname, paramval) => {
	let qqSign = createCommQQSign(`${path}?`, queryname, paramval);
	let postdata = {
		key: context.qqMapKey,
		sig: qqSign
	};
	postdata[queryname] = paramval;

	let [error, result] = await uni.request({
		url: `${baseurl}${path}`,
		data: postdata
	});
	if (!error && result.statusCode == 200) {
		return result.data;
	}
	return null;
}



const createCommQQSign = (url, queryname, paramval) => {
	let params = {
		key: context.qqMapKey,
	};
	params[queryname]=paramval;

	let sortparams = sortObject(params);
	let paramStr = getQuerySortMd5(sortparams);

	paramStr = url + paramStr;
	console.log(paramStr);
	let md5Sign = md5(paramStr);
	console.log(md5Sign);
	return md5Sign;
}


//"https://apis.map.qq.com/ws/geocoder/v1",
const geoCoderMapAsync = async (baseurl, path, location) => {
	let qqSign = createQQSign(`${path}?`, location);
	let [error, result] = await uni.request({
		url: `${baseurl}${path}`,
		data: {
			key: context.qqMapKey,
			location: location,
			sig: qqSign
		}
	});
	if (!error && result.statusCode == 200) {
		return result.data;
	}
	return null;
}



const createQQSign = (url, location) => {
	let params = {
		key: context.qqMapKey,
		location: location
	}

	let sortparams = sortObject(params);
	let paramStr = getQuerySortMd5(sortparams);

	paramStr = url + paramStr;
	console.log(paramStr);
	let md5Sign = md5(paramStr);
	console.log(md5Sign);
	return md5Sign;
}

const getQuerySortMd5 = (sortparams) => {
	let paramStr = ''
	let keys = Object.keys(sortparams)
	for (let idx in keys) {
		let key = keys[idx]
		if (idx == keys.length - 1)
			paramStr += key + '=' + sortparams[key]
		else
			paramStr += key + '=' + sortparams[key] + '&'
	}
	paramStr += 'ce1em0DwZIHdTfEculGHaPqTpcd2AnoW';

	return paramStr;
}

const sortObject = (obj) => {
	let keys = Object.keys(obj).sort()
	let newObj = {}
	for (let i = 0; i < keys.length; i++) {
		newObj[keys[i]] = obj[keys[i]]
	}
	return newObj
}

const mapApiService = {
	geoMultipParamMapAsync,
	geoCommMapAsync,
	initContext,
	geoCoderMapAsync,
	getGPS
	// createQQSign,
	// getQuerySortMd5,
	// sortObject
}

export default mapApiService
